﻿@{
    ViewBag.Title = "Djgl";
    Layout = "~/Views/Shared/_MyLayout.cshtml";
}


@section head{
    <style type="text/css">
        #outermost_container { /*页面最外层容器样式*/
            width: 100%;
            height: 100%;
        }

        /*左右两个容器的样式*/
        #left_container {
            width: 50%;
            height: 100%;
            float: left;
        }

        #right_container {
            width: 50%;
            height: 100%;
            float: left;
        }

        /*上下两个容器的样式*/
        #top_container {
            width: 100%;
            height: 30%;
        }

        #bottom_container {
            width: 100%;
            height: 30%;
        }
    </style>
}

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>单据列表</legend>
</fieldset>

<div id="outermost_container">

    <div id="top_container">
        <form class="layui-form">
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <label class="layui-form-label">单据编号</label>
                    <input type="text" id="SearchText1" name="Title1" required lay-verify="required" onkeypress="getKey()" placeholder="请输入单据编号" class="layui-input" style="width:300px;float:left;">

                    <label class="layui-form-label">产品名称</label>
                    <input type="text" id="SearchText2" name="Title2" required lay-verify="required" onkeypress="getKey()" placeholder="请输入产品名称" class="layui-input" style="width:300px;float:left;">
                    <input type="text" value="" style="display:none" />

                    <button type="button" class="layui-btn layui-btn-normal" style="float:left;" onclick="SearchFunc()"><i class="layui-icon">&#xe615;</i></button>
                </div>
            </div>
        </form>
        <div>
            <!--单据添加功能按钮-->
            <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add" style="margin-left:0.5%" id="BtnAddDj"> 创建 </button>
        </div>
    </div>

    <div id="bottom_container">
        <table id="MaintainTable" lay-filter="MaintainList"></table>
    </div>

</div>


<div id="show_outermost_container" style="display: none; ">
    <div style="margin-top:1%;">

        <form id="DjMsgForm" lay-filter="DjMsgList" class="layui-form">

            <div class="layui-form-item">
                <div style="float:left; margin-left: 5%; width: 45%;">
                    <label class="layui-form-label">产品名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="product_name" lay-verify="required" placeholder="请输入产品名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div style="float:left; margin-right: 5%; width: 45%;">
                    <label class="layui-form-label">产品型号</label>
                    <div class="layui-input-block">
                        <input type="text" name="product_model" placeholder="请输入产品型号" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div style="margin-left: 5%; margin-right: 5%;">
                    <label class="layui-form-label">产品序列号</label>
                    <div class="layui-input-block">
                        <input type="text" name="product_serial" lay-verify="required" placeholder="请输入产品序列号" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>

            <div class="layui-form-item layui-form-text">
                <div style="margin-left: 5%; margin-right: 5%;">
                    <label class="layui-form-label">问题描述</label>
                    <div class="layui-input-block">
                        <textarea name="problem_description" placeholder="请描述问题" class="layui-textarea"></textarea>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div style="margin-left: 5%;">
                    <label class="layui-form-label" style="float: left;">
                        上传图片
                        <i class="layui-icon layui-icon-picture" style="width: 10px; height: 10px;"></i>
                    </label>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <label class="layui-form-label"></label>
                        <button type="button" class="layui-btn" id="BtnSave">提交</button>
                    </div>
                </div>
            </div>

            <button type="submit" id="formAddsubmit" class="layui-btn" lay-submit lay-filter="formAddVerify" style="display:none;">隐藏提交按钮</button>

        </form>

    </div>
</div>


<script type="text/javascript">

    var table;
    layui.use(['form', 'table', 'upload', 'layer'], function () {
        table = layui.table;//AAAA
        var IsValidate = 0;
        var form = layui.form;
        var tab = parent.tab;

        //拦截form提交消息
        form.on('submit(formAddVerify)', function (data) {
            //layer.msg(JSON.stringify(data.field));
            IsValidate = 1;
            return false;
        });

        $("#BtnAddDj").on("click", function () {
            index = layer.open({
                type: 1,
                area: ['80%', '80%'],
                content: $("#show_outermost_container") //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            });
        });

        //新增单据保存
        $("#BtnSave").on("click", function () {
            $("#formAddsubmit").click();
            if (IsValidate == 1) {
                IsValidate = 0;
            } else {
                return;
            }
            var obj = $("#DjMsgForm").serializeArray();
            var data = {};
            $.each(obj, function (i, v) {
                data[v.name] = v.value;
            });
            $.ajax({
                type: "POST",
                url: "/DataExchange/DjAdd",
                async: false,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(data),
                success: function (msg) {
                    if (msg.result == "OK") {
                        layer.msg('新增成功', { icon: 6, time: 1000 });
                        layer.close(index);
                        table.reload("MaintainTable");
                    } else {
                        alert(msg.result);
                    }
                }
            });
        });

        //第一个实例
        table.render({
            elem: '#MaintainTable'
            , method: 'post'
            //, height: 315
            , url: '/DataExchange/Table_Maintain/' //数据接口
            , page: true //开启分页
            , where: { SortName: "maintain_id", Order: "ASC", Condition: " and IsDelete=0" }
            , limits: [10, 50, 100]

            , cols: [[ //表头
                { field: 'maintain_no', title: '单据编号', width: 100, fixed: 'left' }
                , { field: 'product_name', title: '产品名称', width: 150 }
                , { field: 'product_model', title: '产品型号', width: 150 }
                , { field: 'problem_description', title: '问题描述', width: 150 }
                , {
                    field: 'state', title: '单据状态', width: 200,
                    templet: function (d) {
                        if (d.state == "3") {
                            return "结束";
                        }
                        else if (d.state == "2") {
                            return "维修中";
                        }
                        else if (d.state == "1") {
                            return "寄修中";
                        }
                        else if (d.state == "0") {
                            return "申请中";
                        }
                    }
                }
                , {
                    field: "operation", title: "操作", width: 200,
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="Edit">编辑</a><a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>';
                    }
                }
            ]]
        });

        table.on("tool(MaintainList)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'Edit') {

                var options = {
                    id: new Date().getTime(),
                    title: '设备编辑' + data.DeviceId,
                    icon: 'fa-file',
                    url: '/BC/Device/DeviceEditView?DeviceId=' + data.DeviceId
                };
                tab.tabAdd(options);

            }

            if (layEvent == 'TableDel') {
                layer.confirm('<span class="layui-icon">&#x1006;</span>' + '是否真的要删除该单据？', {
                    btn: ['删除', '取消'] //可以无限个按钮
                    , btn1: function (index, layero) {
                        //按钮【删除】的回调
                        //SSEditDelete
                        $.ajax({
                            type: "POST",
                            url: "/DataExchange/Table_DjDel/",
                            async: false,
                            data: { "maintain_id": data.maintain_id },
                            success: function (msg) {
                                if (msg.result == "OK") {
                                    table.reload("MaintainTable", {
                                        //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                                    });
                                    //tab.close(tab.getId());
                                } else {
                                    alert("删除失败");
                                }
                            }
                        });
                        layer.close(index, layero);
                    }
                    , btn2: function (index, layero) {
                        //按钮【按钮三】的回调
                        //alert("2");
                    }
                });
            }

        });
    });

    function SearchFunc() {
        var text1 = $("#SearchText1").val();
        var text2 = $("#SearchText2").val();
        if (text1 == "" && text2 == "") {
            table.reload("MaintainTable", {
                where: { SortName: "maintain_id", Order: "ASC", Condition: " and 1=1" }
                , page: {
                    curr: 1
                }
            });
        }
        else {
            if (text1 != "" && text2 != "") {
                table.reload("MaintainTable", {
                    where: {
                        SortName: "maintain_id", Order: "ASC", Condition: " and maintain_no like '%" + text1 + "%' and product_name like '%" + text2 + "%'"
                    }
                    , page: {
                        curr: 1
                    }
                });
            }
            else if (text1 != "") {
                table.reload("MaintainTable", {
                    where: {
                        SortName: "maintain_id", Order: "ASC", Condition: " and maintain_no like '%" + text1 + "%'"
                    }
                    , page: {
                        curr: 1
                    }
                });
            }
            else {
                table.reload("MaintainTable", {
                    where: {
                        SortName: "maintain_id", Order: "ASC", Condition: " and product_name like '%" + text2 + "%'"
                    }
                    , page: {
                        curr: 1
                    }
                });
            }
        }
    }

</script>
